package com.itheima.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.entity.vo.SetmealVo;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import com.itheima.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService service;
   @Autowired
   private SetmealService setmealService;

    /**
     * 移动端查询
     */
    @GetMapping("/list")
    public  R<Object>  get(Setmeal setmeal){
        System.out.println("进来了   " + setmeal.getCategoryId());
        List<Setmeal>  dishList = setmealService.getByCategoryId(setmeal.getId(),setmeal.getName());

     return  dishList == null ? R.error("查询失败") : R.success(dishList);
    }


    /**
     * 删除 or 批量删除
     */
    @DeleteMapping()
    public R<Object>  delete(@RequestParam("ids") List<Long> ids){
         System.out.println("========="+ids);


    return service.delete(ids) == false ? R.error("删除失败") : R.success("删除成功");
    }


    /**
     * 停售  or 批量停售
     */
    @PostMapping("/status/{status}")
    public R<Object> updateStatus(@PathVariable("status") Integer status,
                             @RequestParam("ids") List<Long> ids){


    return service.updateStatus(status,ids) == false ? R.error("停售失败") : R.success("停售成功");
    }



    /**
     * 修改
     */
    @PutMapping()
    private R<Object> update(@RequestBody SetmealVo setmealVo){
        System.out.println(setmealVo);


        return service.update(setmealVo) == false ? R.error("修改失败") : R.success("修改成功");
    }

    /**
     * 修改回显
     */
    @GetMapping("/{setmealid}")
    public R<Object> updateById(@PathVariable Long setmealid){
        System.out.println(setmealid);
        SetmealVo setmealVo = service.updateById(setmealid);
        return setmealVo ==null ? R.error("修改回显失败"): R.success(setmealVo);
    }


    /**
     * 添加套餐
     */
    @PostMapping()
    public R<Object> add(@RequestBody SetmealVo setmealVo, HttpServletRequest request){

       System.out.println("======="+setmealVo);
       Integer id = (Integer)request.getSession().getAttribute("id");
       setmealVo.setCreateUser(Long.parseLong(String.valueOf(id))); //设置创建人id
       setmealVo.setUpdateUser(Long.parseLong(String.valueOf(id))); //设置修改人id

        if (service.add(setmealVo)) {
           return R.success("添加成功");
        }

           return R.error("添加失败");

    }

    /**
     * 获取全部并显示
     */
    @GetMapping("/page")
    public R<Object> getAll(Integer page,Integer pageSize,String name){
        IPage<SetmealVo> all = service.getAll(name, page, pageSize);

        return all == null ? R.error("查询失败"):R.success(all);

    }

}

